package com.bole.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.bole.common.ALiYunConstant;
import com.bole.data.Result;
import com.bole.data.ResultEnum;
import com.bole.dto.img.DicImgTypeDto;
import com.bole.dto.img.TbCarImgDto;
import com.bole.dto.user.DicCarSpecDto;
import com.bole.feign.DicImgTypeFeign;
import com.bole.feign.TbCarImgFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("img")
@Api(value = "图片管理接口")
public class TbCarImgController {
    @Autowired
    private TbCarImgFeign carImgFeign;
    @Autowired
    private DicImgTypeFeign dicImgTypeFeign;


    @RequestMapping("tolist")
    public String toList(Model model){
        List<DicImgTypeDto> list = dicImgTypeFeign.getImgTypeInfo();
        model.addAttribute("list",list);
        return "img";
    }



    @GetMapping("getImgInfo")
    @ApiOperation(value = "查询图片")
    @ResponseBody
    public Result getImgInfo(){
        List<TbCarImgDto> list =  carImgFeign.getImgInfo();
        return Result.response(ResultEnum.OK).setData(list);
    }

    @PostMapping("saveImgInfo")
    @ApiOperation(value = "增加图片信息")
    @ApiImplicitParam(name = "tbCarImgDto",value = "图片对象",required = true,dataTypeClass = TbCarImgDto.class)
    @ResponseBody
    public Result saveImgTypeInfo(@RequestBody TbCarImgDto tbCarImgDto){
        carImgFeign.saveImgInfo(tbCarImgDto);
        return Result.response(ResultEnum.OK);
    }


    //上传图片
    @RequestMapping("upload")
    @ResponseBody
    public Result upload(@RequestParam(value = "myFile") MultipartFile myFile){
        try{
            // <yourObjectName>上传文件到OSS时需要指定包含文件后缀在内的完整路径，例如abc/efg/123.jpg。
            String objectName = myFile.getOriginalFilename();//上传时的图片路径
            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(ALiYunConstant.ENDPOINT, ALiYunConstant.ACCESSKEYID, ALiYunConstant.ACCESSKEYSECRET);
            // 上传内容到指定的存储空间（bucketName）并保存为指定的文件名称（objectName）。
//        String content = "Hello OSS";
            String url = null;
            try {
                ossClient.putObject(ALiYunConstant.BUCKETNAME, objectName, myFile.getInputStream());
                //拼接图片访问地址
                url = "http://"+ALiYunConstant.BUCKETNAME+ALiYunConstant.POINT+objectName;
            } catch (IOException e) {
                e.printStackTrace();
                return Result.response(ResultEnum.FAIL);
            }
            // 关闭OSSClient。
            ossClient.shutdown();
            return Result.response(ResultEnum.OK).setData(url);
        }catch (Exception e){
            return Result.response(ResultEnum.FAIL);
        }
    }


    @PostMapping("toUpdate/{id}")
    @ApiOperation(value = "回显")
    @ApiImplicitParam(name = "id",value = "图片id",required = true,dataTypeClass = Long.class)
    @ResponseBody
    public Result toUpdate(@PathVariable("id")Long id){
        TbCarImgDto carImgDto = carImgFeign.toUpdate(id);
        return Result.response(ResultEnum.OK).setData(carImgDto);
    }


    @PostMapping("updateImgInfo")
    @ApiOperation(value = "修改信息")
    @ApiImplicitParam(name = "dicImgTypeDto",value = "图片对象",required = true,dataTypeClass = TbCarImgDto.class)
    @ResponseBody
    public Result updateImgInfo(@RequestBody TbCarImgDto carImgDto){
        carImgFeign.updateImgInfo(carImgDto);
        return Result.response(ResultEnum.OK);
    }


    @PostMapping("deleteBatch/{ids}")
    @ApiOperation(value = "批量删除信息",notes = "批量删除信息")
    @ApiImplicitParam(name = "ids",value = "图片id",required = true,paramType = "path",dataTypeClass = Long.class)
    @ResponseBody
    public Result deleteBatch(@PathVariable("ids")Long[] ids){
        List<Long> idss = new ArrayList<>();
        for (Long id : ids) {
            idss.add(id);
        }
        carImgFeign.deleteBatch(idss);
        return Result.response(ResultEnum.OK);
    }


}
